package com.qf.service.impl;

import com.qf.dao.AccountDao;
import com.qf.pojo.Account;
import com.qf.service.AccountService;
import org.springframework.stereotype.Service;

/**
 * @ClassName AccountServiceImpl
 * @Description TODO
 * @Author Windy
 * @Date 2022/8/31 10:14
 * @Version 1.0
 **/
@Service
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    //使用set方法注入
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void transfer(String sourceName, String targetName, Double money)  {
        try {
            //根据名称查询出转账用户
            Account sourceAccount = accountDao.findAccountByName(sourceName);
            //根据名称查询转入账用户
            Account targetAccount = accountDao.findAccountByName(targetName);
            //转出账户扣钱
            sourceAccount.setMoney(sourceAccount.getMoney() - money);
            //转入账户加钱
            targetAccount.setMoney(targetAccount.getMoney() + money);
            //更新转出账户
            accountDao.updateAccount(sourceAccount);
            int i = 1/0;
            //更新转入账户
            accountDao.updateAccount(targetAccount);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
